<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('管理')"/>
</head>
<body class="hold-transition skin-blue sidebar-mini layout-boxed">
<div class="wrapper">
    <th:block th:include="include :: headbar"/>
    <th:block th:include="include :: sidebar"/>
    <div class="content-wrapper">
        <section class="content-header">
            <h1>&nbsp;</h1>
            <ol class="breadcrumb">
                <li><a href="#"><i class="fa fa-dashboard"></i> 首页</a></li>
                <li><a href="#">ADAS订单管理</a></li>
                <li class="active">ADAS订单</li>
            </ol>
        </section>
        <section class="content">
            <div class="box box-solid">
                <form class="box-header with-border form-inline" id="form">
                    <input type="text" class="form-control input-sm" placeholder="设备ID" name="deviceId">
                    <button type="button" class="btn btn-success btn-sm btn-flat" onclick="$.table.search('form')"><i
                            class="fa fa-search"></i> 搜索
                    </button>
                    <button type="button" class="btn btn-primary btn-sm btn-flat" onclick="download()"><i
                            class="glyphicon glyphicon-download"></i> 导出
                    </button>
                </form>
                <div class="box-body">
                    <table id="bootstrap-table" data-mobile-responsive="true"></table>
                </div>
            </div>
        </section>
    </div>
    <th:block th:include="include :: copyright"/>
</div>
<th:block th:include="include :: footer"/>
<script th:inline="javascript">
    var prefix = "/car/device";
    $(function () {
        var options = {
            url: prefix + "/list",
            createUrl: prefix + '/add',
            removeUrl: prefix + '/remove',
            updateUrl: prefix + '/edit/{id}',
            sortName: "",
            uniqueId: 'id',
            modalName: "",
            modalWidth: 350,
            modalHeight: 400,
            columns: [
                {radio: true},
                {
                    title: "序号", formatter: function (value, row, index) {
                        return $.table.serialNumber(index);
                    }
                },
                {
                    field: 'deviceId', title: '设备ID', undefinedText: '-',
                    formatter: function (value, row, index) {
                        if (value === '') {
                            return 'undefined deviceId'
                        } else {
                            return value;
                        }
                    }
                },
                {field: 'deviceProperties', title: '设备属性'},
                {field: 'deviceActiveStatus', title: '设备激活状态', formatter: statusFormatter},
                {field: 'deviceAdas', title: '设备ADAS激活码'},
                {field: 'deviceModelNum', title: '设备车模数量'}
            ]
        };
        $.table.init(options);
    });

    function statusFormatter(value, row, index) {
        if (value === 1) {
            return '<span class="label label-success">BSD</span><span class="label label-warning">未激活</span><span class="label label-warning">未激活</span>';
        } else if (value === 12) {
            return '<span class="label label-success">BSD</span><span class="label label-success">MOD</span><span class="label label-warning">未激活</span>';
        } else if (value === 13) {
            return '<span class="label label-success">BSD</span><span class="label label-warning">未激活</span><span class="label label-success">LDW</span>';
        } else if (value === 123) {
            return '<span class="label label-success">BSD</span><span class="label label-success">MOD</span><span class="label label-success">LDW</span>';
        } else if (value === 2) {
            return '<span class="label label-warning">未激活</span><span class="label label-success">MOD</span><span class="label label-warning">未激活</span>';
        } else if (value === 3) {
            return '<span class="label label-warning">未激活</span><span class="label label-warning">未激活</span><span class="label label-success">LDW</span>';
        } else if (value === 23) {
            return '<span class="label label-warning">未激活</span><span class="label label-success">MOD</span><span class="label label-success">LDW</span>';
        } else if (value === 123) {
            return '<span class="label label-success">BSD</span><span class="label label-success">MOD</span><span class="label label-success">LDW</span>';
        } else {
            return '<span class="label label-warning">未激活</span><span class="label label-warning">未激活</span><span class="label label-warning">未激活</span>';
        }
    }

    function download() {
        var oReq = new XMLHttpRequest();
        var date = new Date();
        var month = date.getMonth() + 1;
        var now = date.getFullYear() + "-" + month + "-" + date.getDate();
        oReq.open("GET", prefix + "/export/order", true);
        oReq.responseType = "blob";
        oReq.onload = function (oEvent) {
            var content = oReq.response;

            var elink = document.createElement('a');
            elink.download = "ADAS订单报表" + now
                + ".xlsx";
            elink.style.display = 'none';

            var blob = new Blob([content], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8'});
            elink.href = URL.createObjectURL(blob);

            document.body.appendChild(elink);
            elink.click();

            document.body.removeChild(elink);
            window.URL.revokeObjectURL(elink.href);
        };
        oReq.send();
    }


    // function disable(deviceId) {
    //     $.post(prefix + "/changeStatus", { "deviceId": deviceId, "status": 0 }, function(resp){
    //         $.table.refresh();
    //     });
    // }
    //
    // function enable(deviceId) {
    //     $.post(prefix + "/changeStatus", { "deviceId": deviceId, "status": 1 }, function(resp){
    //         $.table.refresh();
    //     });
    // }
</script>
</body>
</html>
